package com.hmdp.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    
    // 定义交换机名称
    public static final String SECKILL_EXCHANGE = "seckill.exchange";
    
    // 定义队列名称
    public static final String SECKILL_QUEUE = "seckill.queue";
    
    // 定义路由键
    public static final String SECKILL_ROUTING_KEY = "seckill.order";
    
    /**
     * 声明交换机
     * @return DirectExchange
     */
    @Bean
    public Exchange seckillExchange() {
        return ExchangeBuilder.directExchange(SECKILL_EXCHANGE)
                .durable(true)  // 持久化
                .build();
    }
    
    /**
     * 声明队列
     * @return Queue
     */
    @Bean
    public Queue seckillQueue() {
        return QueueBuilder.durable(SECKILL_QUEUE)
                .build();
    }
    
    /**
     * 绑定交换机和队列
     * @return Binding
     */
    @Bean
    public Binding seckillBinding() {
        return BindingBuilder.bind(seckillQueue())
                .to(seckillExchange())
                .with(SECKILL_ROUTING_KEY)
                .noargs();
    }
}